package com.htmlunit;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlDivision;
import com.gargoylesoftware.htmlunit.html.HtmlImage;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.xiaoleilu.hutool.http.HttpUtil;
import com.xiaoleilu.hutool.io.FileUtil;

public class Htmlunit {
	public static void main(String[] args) throws Exception{
		osChinaImages();
	}
	
	
	//os china --博客列表:
	public static void osChinaBlogs() throws Exception{
		final WebClient webClient=new WebClient();
		webClient.getOptions().setCssEnabled(false);
		webClient.getOptions().setJavaScriptEnabled(false);
		final HtmlPage page=webClient.getPage("http://www.oschina.net/");
		List<?> list =page.getByXPath("/html/body/section/section/article/div[4]/section/div[1]/div");
		for (Object o :list){
			HtmlDivision div = (HtmlDivision) o;
			HtmlAnchor  anchor = (HtmlAnchor)div.getByXPath("a").get(0); 
			System.out.println("tile:"+anchor.asText()+ "href:"+anchor.getHrefAttribute());
		}
		webClient.close();
	}
	
	public static void osChinaImages() throws Exception{
		final WebClient webClient=new WebClient();
		webClient.getOptions().setCssEnabled(false);
		webClient.getOptions().setJavaScriptEnabled(false);
		final HtmlPage page=webClient.getPage("http://www.oschina.net/");
		List<?> list =page.getByXPath("/html/body/section/section/article/div[4]/section/div[2]/div");
		System.out.println(list.size());
		for (Object o :list){
			HtmlDivision div = (HtmlDivision) o;
			HtmlImage  image = (HtmlImage)div.getByXPath("a[1]/figure/img").get(0); 
			String imageLink =  image.getSrcAttribute();
			System.out.println("image link:" + imageLink );
			download(imageLink, System.currentTimeMillis()+".png","c:/fabu");
		}
		webClient.close();
	}
	
	
	public static void qiubai() throws Exception{
		final WebClient webClient=new WebClient();
		webClient.getOptions().setCssEnabled(false);
		webClient.getOptions().setJavaScriptEnabled(false);
		final HtmlPage page=webClient.getPage("http://www.qiushibaike.com/");
		List<?> list =page.getByXPath("/html/body/section/section/article/div[4]/section/div[2]/div");
		System.out.println(list.size());
		for (Object o :list){
			HtmlDivision div = (HtmlDivision) o;
			HtmlImage  image = (HtmlImage)div.getByXPath("a[1]/figure/img").get(0); 
			String imageLink =  image.getSrcAttribute();
			System.out.println("image link:" + imageLink );
			download(imageLink, System.currentTimeMillis()+".png","c:/fabu");
		}
		webClient.close();
	}
	
	 public static void download(String urlString, String filename,String savePath) throws Exception {  
	        // 构造URL  
	        URL url = new URL(urlString);  
	        // 打开连接  
	        URLConnection con = url.openConnection();  
	        //设置请求超时为5s  
	        con.setConnectTimeout(5*1000);  
	        // 输入流  
	        InputStream is = con.getInputStream();  
	      
	        // 1K的数据缓冲  
	        byte[] bs = new byte[1024];  
	        // 读取到的数据长度  
	        int len;  
	        // 输出的文件流  
	       File sf=new File(savePath);  
	       if(!sf.exists()){  
	           sf.mkdirs();  
	       }  
	       OutputStream os = new FileOutputStream(sf.getPath()+"\\"+filename);  
	        // 开始读取  
	        while ((len = is.read(bs)) != -1) {  
	          os.write(bs, 0, len);  
	        }  
	        // 完毕，关闭所有链接  
	        os.close();  
	        is.close();  
	    }   

}
